﻿Imports Telerik.Windows.Controls
Imports Telerik.Windows.Data
Imports CRM.BusinessLayer
Imports System.Data
Imports System.Text
Partial Public Class frmAdminPaymentMethods
    Private intSaveNeworEdit As Integer
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmAdminPaymentMethods_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        refreshList()
        txtPaymentMethod.IsEnabled = False
    End Sub

    Private Sub refreshList()
        Try
            Me.grdPaymentMethods.ItemsSource = Admin.PaymentMethodsList
            Me.grdPaymentMethods.Columns(0).IsVisible = False
            Me.grdPaymentMethods.Columns(1).Width = New GridLength(150)
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminPaymentMethods-refreshList")
            Exit Sub
        End Try

        'Me.grdWhereHear.Columns(1).Width = New GridLength(140)
        'Me.grdQualifications.Columns(3).IsVisible = False
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        intSaveNeworEdit = 1
        ClearControls()
        Me.btnAddNew.IsEnabled = False
        Me.btnSave.IsEnabled = True
        Me.btnEdit.IsEnabled = False
        'Me.btnCancel.IsEnabled = True
        Me.btnDelete.IsEnabled = False
        Me.txtPaymentMethod.IsEnabled = True
        Me.txtPaymentMethod.Focus()
    End Sub

    Private Sub ClearControls()
        Me.txtPaymentMethod.Text = String.Empty
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

        If txtPaymentMethod.Text.Trim = String.Empty Then
            CommonFuncs.DisplayMessage("Please enter Payment Method", CommonDialog.CustomDialogIcons.Information, "Error")
            txtPaymentMethod.Focus()
            Exit Sub

        Else
            btnSave.IsEnabled = False
            Try
                Dim Sql As New StringBuilder
                Select Case intSaveNeworEdit
                    Case 1 'new one
                        Sql = New StringBuilder
                        With Sql
                            .Append("")
                            .Append("insert into A_PaymentMethods ")
                            .Append("  (PaymentMethod_Name)")
                            .Append(" Values(")
                            .Append(" '" & Me.txtPaymentMethod.Text & "')")
                        End With

                        If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                            refreshList()
                            CommonFuncs.DisplayMessage("The Payment Type has been added successfully.", CommonDialog.CustomDialogIcons.Information, "Success")
                            'Exit Sub
                        Else
                            CommonFuncs.DisplayMessage("There is an error adding the Payment Type to the system. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                            'Exit Sub
                        End If
                    Case 2 'existing one
                        Sql = New StringBuilder
                        With Sql
                            .Append("update A_PaymentMethods set ")
                            .Append(" PaymentMethod_Name = '" & Me.txtPaymentMethod.Text & "'")
                            Dim selectedGroup As DataRow = grdPaymentMethods.SelectedItem()
                            .Append(" where PaymentMethod_ID = " & selectedGroup(0))
                        End With

                        If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                            refreshList()
                            CommonFuncs.DisplayMessage("The Payment Type has been updated successfully.", CommonDialog.CustomDialogIcons.Information, "Success")
                            'Exit Sub
                        Else
                            CommonFuncs.DisplayMessage("There is an error in editing the Payment Type details. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                            'Exit Sub
                        End If

                End Select

            Catch ex As Exception
                CommonFuncs.ErrorHandler(ex, "frmAdminPaymentTypes-btnSave_click")
                Exit Sub
            End Try

        End If

        ClearControls()
        txtPaymentMethod.IsEnabled = False
        btnAddNew.IsEnabled = True
        btnEdit.IsEnabled = False
        btnSave.IsEnabled = False
        'End If

    End Sub

    Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        intSaveNeworEdit = 2
        'ClearControls()
        Me.txtPaymentMethod.IsEnabled = True
        Dim selectedGroup As DataRow = grdPaymentMethods.SelectedItem
        If Not selectedGroup Is Nothing Then
            Me.btnSave.IsEnabled = True
            ' Me.btnCancel.IsEnabled = True
            Me.btnDelete.IsEnabled = False
            Me.btnEdit.IsEnabled = False
            Me.txtPaymentMethod.Text = selectedGroup(1).ToString()
            Me.txtPaymentMethod.Focus()
        Else
            CommonFuncs.DisplayMessage("Selecet the Payment type to Edit", CommonDialog.CustomDialogIcons.Information, "Select")
        End If

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            Dim deleteOption As MsgBoxResult
            deleteOption = MsgBox("Are you really sure to delete This Payment Method ?", MsgBoxStyle.OkCancel)
            If deleteOption = MsgBoxResult.Ok Then
                Dim selectedRow As DataRow = grdPaymentMethods.SelectedItem
                Dim Sql As New StringBuilder
                With Sql
                    .Append("delete  A_PaymentMethods where PaymentMethod_ID=" & selectedRow(0).ToString())
                End With

                If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                    btnDelete.IsEnabled = False
                    'btnCancel.IsEnabled = False
                    btnEdit.IsEnabled = False
                    btnSave.IsEnabled = False
                    'grdWhereHear.IsEnabled = True
                    ClearControls()
                    refreshList()
                    CommonFuncs.DisplayMessage("The Payment method have been deleted successfully.", CommonDialog.CustomDialogIcons.Information, "Success")
                Else
                    CommonFuncs.DisplayMessage("There is an error while deleting the Payment method from the system. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                    'Exit Sub
                End If

            ElseIf deleteOption = MsgBoxResult.Cancel Then

            End If
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminPaymentMethods-btnDelete_Click")
            Exit Sub
        End Try

    End Sub
    Private Sub grdPaymentMethods_SelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs)
        Me.btnDelete.IsEnabled = True
        Me.btnEdit.IsEnabled = True
        Dim selectedRow As DataRow = grdPaymentMethods.SelectedItem
        If (grdPaymentMethods.SelectedItems.Count > 0) Then
            Me.txtPaymentMethod.Text = Convert.ToString(selectedRow("PaymentMethod_Name"))
        End If
    End Sub
End Class
